﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoArticle : DaoBase
    {

        public DaoArticle()
            : base()
        {
            this.TableName = "article";
            this.ViewName = "article";
        }

        public Article SelectById(Int32 OrionId)
        {
            lParam.Clear();
            Article retour = new Article();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@OrionId", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public List<Article> SelectAllActif()
        {
            lParam.Clear();
            List<Article> retour = new List<Article>();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionEtat=1";

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        public List<Article> SelectAll()
        {
            lParam.Clear();
            List<Article> retour = new List<Article>();
            String req = this.DebutSelect
            + this.TableName;

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        private Article ChargeFromReader(MySqlDataReader dr)
        {
            int i = 0;
            Article retour = new Article();
            retour.OrionId = dr.GetInt32("OrionId");

            i++;
            if (dr.IsDBNull(i))
                retour.CodeBarre = String.Empty;
            else
                retour.CodeBarre = dr.GetString("CodeBarre");
            i++;
            if (dr.IsDBNull(i))
                retour.Libelle = String.Empty;
            else
                retour.Libelle = dr.GetString("Libelle");
            i++;
            if (dr.IsDBNull(i))
                retour.PrixAchat = 0.0;
            else
                retour.PrixAchat = dr.GetDouble("PrixAchat");
            i++;
            if (dr.IsDBNull(i))
                retour.PrixVente = 0.0;
            else
                retour.PrixVente = dr.GetDouble("PrixVente");
            i++;
            if (dr.IsDBNull(i))
                retour.Reference = String.Empty;
            else
                retour.Reference = dr.GetString("Reference");
            i++;
            if (dr.IsDBNull(i))
                retour.Commentaire = String.Empty;
            else
                retour.Commentaire = dr.GetString("Commentaire");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }

        public Article Update(Article oe)
        {
            lParam.Clear();
            oe.OrionMaj = DateTime.Now;

            Article retour = new Article();
            String req = "UPDATE " + TableName + " SET CodeBarre=@CodeBarre,Libelle=@Libelle,PrixAchat=@PrixAchat,PrixVente=@PrixVente,Reference=@Reference,Commentaire=@Commentaire,OrionUserId=@OrionUserId, OrionMaj=@OrionMaj WHERE OrionId=@OrionId";

            lParam.Add(new MySqlParameter("@CodeBarre", oe.CodeBarre));
            lParam.Add(new MySqlParameter("@Libelle", oe.Libelle));
            lParam.Add(new MySqlParameter("@PrixAchat", oe.PrixAchat));
            lParam.Add(new MySqlParameter("@PrixVente", oe.PrixVente));
            lParam.Add(new MySqlParameter("@Reference", oe.Reference));
            lParam.Add(new MySqlParameter("@Commentaire", oe.Commentaire));

            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionId", oe.OrionId));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(oe.OrionId);

            return retour;
        }

        public Article Insert(Article oe)
        {
            lParam.Clear();
            oe.OrionCrea = DateTime.Now;
            oe.OrionMaj = DateTime.Now;

            Article retour = new Article();
            String req = "INSERT INTO " + TableName + "(OrionId,CodeBarre,Libelle,PrixAchat,PrixVente,Reference,Commentaire,OrionEtat, OrionUserId, OrionCrea, OrionMaj)  VALUES(NULL,@CodeBarre,@Libelle,@PrixAchat,@PrixVente,@Reference,@Commentaire,@OrionEtat,@OrionUserId,@OrionCrea,@OrionMaj)";
            lParam.Add(new MySqlParameter("@CodeBarre", oe.CodeBarre));
            lParam.Add(new MySqlParameter("@Libelle", oe.Libelle));
            lParam.Add(new MySqlParameter("@PrixAchat", oe.PrixAchat));
            lParam.Add(new MySqlParameter("@PrixVente", oe.PrixVente));
            lParam.Add(new MySqlParameter("@Reference", oe.Reference));
            lParam.Add(new MySqlParameter("@Commentaire", oe.Commentaire));
            lParam.Add(new MySqlParameter("@OrionEtat", oe.OrionEtat));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionCrea", oe.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }
    }
}